//
//  W_RegistViewController.m
//  Go!
//
//  Created by 凌風 on 16/7/20.
//  Copyright © 2016年 凌风. All rights reserved.
//

#import "W_RegistViewController.h"

@interface W_RegistViewController ()

@property (nonatomic, strong) UIView *bgView;/**< 背景视图 */
@property (nonatomic, strong) UITextField *accountTextField; /**< 账号输入框 */
@property (nonatomic, strong) UITextField *passwordTextField;/**< 密码输入框 */
@property (nonatomic, strong) UITextField *againPasswordTextField;/**< 再次输入密码输入框 */
@property (nonatomic, strong) UIButton *registBtn;/**< 注册按钮 */

@end

@implementation W_RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initializeDataSource];
    [self initializeUserInterface];
}

- (void)initializeDataSource {
    
}

- (void)initializeUserInterface {
    self.topTitle = @"注册";
    
    [self.view addSubview:self.bgView];
    [self.bgView addSubview:self.accountTextField];
    [self.bgView addSubview:self.passwordTextField];
    [self.bgView addSubview:self.againPasswordTextField];
    
    [self.view addSubview:self.registBtn];
}


#pragma mark -- 监听事件的回调方法
- (void)respondsToPasswordTextField:(UITextField *)textField {
    if (self.accountTextField.text.length == 0) {
        [Tools showAlertWithMessage:@"请先输入账号" ViewController:self];
        textField.text = @"";
    }
}

- (void)respondsToAgainPasswordTextField:(UITextField *)textField {
    if (self.accountTextField.text.length == 0) {
        [Tools showAlertWithMessage:@"请先输入账号" ViewController:self];
        textField.text = @"";
        return;
    }
    
    if (self.passwordTextField.text.length == 0) {
        [Tools showAlertWithMessage:@"请先输入密码" ViewController:self];
        textField.text = @"";
        return;
    }
    
    if (self.againPasswordTextField.text.length > 0) {
        self.registBtn.selected = true;
        [self.registBtn setBackgroundColor:[UIColor redColor]];
    } else {
        self.registBtn.selected = false;
        [self.registBtn setBackgroundColor:[UIColor colorWithWhite:0.85 alpha:1]];
    }

}

- (void)respondsToRegistBtn:(UIButton *)sender {
    if (self.registBtn.selected){
        [[NSNotificationCenter defaultCenter] postNotificationName:@"postUserInfo" object:@{@"userName":self.accountTextField.text} userInfo:@{@"user":@"registVc"}];
        
        [self.presentingViewController.presentingViewController dismissViewControllerAnimated:true completion:nil];
        
    } else if (![self.passwordTextField.text isEqualToString:self.againPasswordTextField.text]) {
        [Tools showAlertWithMessage:@"两次输入的密码不一致" ViewController:self];
        return;
    }
}

#pragma mark -- Getter
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = ({
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.topView.frame) + Size_H(10), ScreenW, Size_H(151))];
            
            view.backgroundColor = [UIColor whiteColor];
            
            
            UIImageView *accountImageView = [[UIImageView alloc] initWithFrame:CGRectMake(Size_W(8), Size_H(8), Size_W(34), Size_H(34))];
            
            accountImageView.image = [UIImage imageNamed:@"注册_账号.png"];
            
            [view addSubview:accountImageView];
            
            
            UIView *lineView1 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(accountImageView.frame) + Size_H(8), ScreenW, Size_H(0.5))];
            lineView1.backgroundColor = [UIColor lightGrayColor];
            
            [view addSubview:lineView1];
            
            UIImageView *passwordImageView = [[UIImageView alloc] initWithFrame:CGRectMake(Size_W(8), CGRectGetMaxY(lineView1.frame) + Size_H(8), Size_W(34), Size_H(34))];
            
            passwordImageView.image = [UIImage imageNamed:@"注册_密码.png"];
            
            [view addSubview:passwordImageView];
            
            UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(passwordImageView.frame) + Size_H(8), ScreenW, Size_H(0.5))];
            lineView2.backgroundColor = [UIColor lightGrayColor];
            
            [view addSubview:lineView2];
            
            UIImageView *againPasswordImageView = [[UIImageView alloc] initWithFrame:CGRectMake(Size_W(8), CGRectGetMaxY(lineView2.frame) + Size_H(8), Size_W(34), Size_H(34))];
            
            againPasswordImageView.image = [UIImage imageNamed:@"注册_再次输入密码.png"];
            
            [view addSubview:againPasswordImageView];
            
            
            
            view;
        });
    }
    
    return _bgView;
}

- (UITextField *)accountTextField {
    if (!_accountTextField) {
        _accountTextField = ({
            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(Size_W(50), Size_H(8), ScreenW - Size_W(50), Size_H(34))];
            
            //            textField.placeholder = @"请输入手机号";
            textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入账号" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:Font(17)]}];
            
            textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            textField.tintColor = [UIColor redColor];
            
            textField;
        });
    }
    
    return _accountTextField;
}

- (UITextField *)passwordTextField {
    if (!_passwordTextField) {
        _passwordTextField = ({
            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(Size_W(50), Size_H(58.5), ScreenW - Size_W(50), Size_H(34))];
            
            textField.placeholder = @"请输入密码";
            textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:Font(17)]}];
            
            textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            textField.tintColor = [UIColor redColor];

            
            [textField addTarget:self action:@selector(respondsToPasswordTextField:) forControlEvents:UIControlEventEditingChanged];
            
            textField;
        });
    }
    
    return _passwordTextField;
}

- (UITextField *)againPasswordTextField {
    if (!_againPasswordTextField) {
        _againPasswordTextField = ({
            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(Size_W(50), Size_H(109), ScreenW - Size_W(50), Size_H(34))];
            
//            textField.placeholder = @"请输入密码";
            textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请再次输入密码" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:Font(17)]}];
            
            textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            textField.tintColor = [UIColor redColor];
//            textField.keyboardType = UIKeyboardTypePhonePad;
            
            [textField addTarget:self action:@selector(respondsToAgainPasswordTextField:) forControlEvents:UIControlEventEditingChanged];
            
            textField;
        });
    }
    
    return _againPasswordTextField;
}

- (UIButton *)registBtn {
    if (!_registBtn) {
        _registBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _registBtn.frame = CGRectMake(Size_W(8), CGRectGetMaxY(self.bgView.frame) + Size_H(8), ScreenW - Size_W(16), Size_H(44));
        [_registBtn setTitle:@"注 册" forState:UIControlStateNormal];
        _registBtn.titleLabel.font = SystemFont;
        
        // title颜色
        [_registBtn setTitleColor:[UIColor colorWithWhite:0.65 alpha:1] forState:UIControlStateNormal];
        [_registBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        
        // 背景色
        [_registBtn setBackgroundColor:[UIColor colorWithWhite:0.85 alpha:1]];
        
        [_registBtn addTarget:self action:@selector(respondsToRegistBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    
    return _registBtn;
}

@end
